#ifndef CUSTOMER_HH
#define CUSTOMER_HH

#include <string>
#include <memory>
#include <fstream>
#include <set>
#include "Account.hh"

namespace BankSystem {
    // 客户类
    class Customer {
    private:
        std::string name;
        std::shared_ptr<SavingsAccount> savingsAccount;
        std::shared_ptr<CheckingAccount> checkingAccount;
        
    public:
        Customer(const std::string& customerName, 
                 const std::shared_ptr<SavingsAccount>& savings, 
                 const std::shared_ptr<CheckingAccount>& checking)
            : name(customerName), savingsAccount(savings), checkingAccount(checking) {}
        
        // Getters
        std::string getName() const { return name; }
        std::shared_ptr<SavingsAccount> getSavingsAccount() const { return savingsAccount; }
        std::shared_ptr<CheckingAccount> getCheckingAccount() const { return checkingAccount; }
        
        // 查看客户的所有交易
        void viewAllTransactions() const {
            const auto& savingsTxs = savingsAccount->getTransactions();
            const auto& checkingTxs = checkingAccount->getTransactions();
            
            if (savingsTxs.empty() && checkingTxs.empty()) {
                std::cout << "No transactions found for this customer." << std::endl;
                return;
            }
            
            std::cout << "\n===== Transactions for Customer: " << name << " =====\n" << std::endl;
            
            // 合并两个账户的交易并按时间排序
            std::vector<std::shared_ptr<Transaction>> allTransactions;
            allTransactions.insert(allTransactions.end(), savingsTxs.begin(), savingsTxs.end());
            allTransactions.insert(allTransactions.end(), checkingTxs.begin(), checkingTxs.end());
            
            // 按时间排序（为简化起见，这里直接使用交易ID，假设ID是按时间顺序生成的）
            std::sort(allTransactions.begin(), allTransactions.end(), 
                [](const std::shared_ptr<Transaction>& a, const std::shared_ptr<Transaction>& b) {
                    return a->getTransactionId() < b->getTransactionId();
                });
            
            // 移除重复的交易（转账会在两个账户中各记录一次）
            std::set<std::string> processedIds;
            
            for (const auto& transaction : allTransactions) {
                if (processedIds.find(transaction->getTransactionId()) == processedIds.end()) {
                    std::cout << "--------------------------------------" << std::endl;
                    std::cout << transaction->getDetails() << std::endl;
                    processedIds.insert(transaction->getTransactionId());
                }
            }
        }
        
        // 生成客户报告
        bool generateReport() const {
            const auto& savingsTxs = savingsAccount->getTransactions();
            const auto& checkingTxs = checkingAccount->getTransactions();
            
            if (savingsTxs.empty() && checkingTxs.empty()) {
                std::cout << "No transactions to report for this customer." << std::endl;
                return false;
            }
            
            std::string cleanName = name;
            // 替换名字中的空格为下划线，以便用于文件名
            std::replace(cleanName.begin(), cleanName.end(), ' ', '_');
            
            std::string filename = "transactions_" + cleanName + "_" + Utils::getCurrentDate() + ".txt";
            std::ofstream reportFile(filename);
            
            if (!reportFile.is_open()) {
                std::cout << "Failed to create report file." << std::endl;
                return false;
            }
            
            reportFile << "====== Transaction Report for Customer: " << name << " ======\n\n";
            reportFile << "Savings Account: " << savingsAccount->getAccountId() << "\n";
            reportFile << "Current Balance: " << Utils::formatCurrency(savingsAccount->getBalance()) << "\n\n";
            reportFile << "Checking Account: " << checkingAccount->getAccountId() << "\n";
            reportFile << "Current Balance: " << Utils::formatCurrency(checkingAccount->getBalance()) << "\n\n";
            reportFile << "Transaction History:\n\n";
            
            // 合并两个账户的交易并按时间排序
            std::vector<std::shared_ptr<Transaction>> allTransactions;
            allTransactions.insert(allTransactions.end(), savingsTxs.begin(), savingsTxs.end());
            allTransactions.insert(allTransactions.end(), checkingTxs.begin(), checkingTxs.end());
            
            // 按时间排序（为简化起见，这里直接使用交易ID，假设ID是按时间顺序生成的）
            std::sort(allTransactions.begin(), allTransactions.end(), 
                [](const std::shared_ptr<Transaction>& a, const std::shared_ptr<Transaction>& b) {
                    return a->getTransactionId() < b->getTransactionId();
                });
            
            // 移除重复的交易（转账会在两个账户中各记录一次）
            std::set<std::string> processedIds;
            
            for (const auto& transaction : allTransactions) {
                if (processedIds.find(transaction->getTransactionId()) == processedIds.end()) {
                    reportFile << "--------------------------------------\n";
                    reportFile << transaction->getDetails() << "\n";
                    processedIds.insert(transaction->getTransactionId());
                }
            }
            
            reportFile.close();
            
            std::cout << "Report successfully generated as \"" << filename << "\"" << std::endl;
            return true;
        }
    };
}

#endif // CUSTOMER_H